1060B - Maximum Sum of Digits - CodeForces Solution


greedy *1100

Please click on ads to support us..

Python Code:

n = int(input())

def max_input(n):
    if (n < 10):
        return n
    else:
        length = len(str(n)) - 1
        first_part = int("9" * length)
        second_part = (n - first_part)
        
                sum_first_part = 9 * length
        sum_second_part = 0

        for digit in str(second_part): 
            sum_second_part += int(digit)   

        result = sum_first_part + sum_second_part

        return result
        
        
print(max_input(n))

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define fast_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define ll long long
#define ull unsigned ll
#define nrbits __builtin_popcount
#define nrbitsll __builtin_popcountll
#define int ll

const int dim = 2e5+1;
const int mod = 1e9+7;//998244353;
const double pi = 3.14159265359;
const int INF = INT_MAX;

// problema pe care o rezolv : https://www.pbinfo.ro/probleme/2485/nxy (cerinta 1)

int sum (int x)
{
    int ret = 0;
    while (x)
        ret += x % 10, x /= 10;
    return ret;
}

void solve()
{
    int n;
    cin >> n;

    int x = 0;

    while (x * 10 + 9 <= n)
        x = x * 10 + 9;

    cout << sum(x) + sum(n - x); // multumim enderdragon pentru aceasta pb mirobolanta!
}

signed main()
{
    fast_io;
    int t = 1;
    //cin >> t;

    for (int tc = 1; tc <= t; tc++)
        {
            //cout << "Case #" << tc << ": \n";
            solve(), cout << '\n';
        }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node